package csel.controller.commands;

import java.io.FileNotFoundException;
import java.io.IOException;

import javax.swing.JOptionPane;

import csel.model.*;
import csel.view.*;

public class LoadCommand implements GameCommand
{
	private Model model;
	private StatusViewport statusViewport;
	private AreaViewport areaViewport;
	private Splasher splashViewport;
	private String fileName;
	private GameCommand gameCommand;
	
	//constructs a new load command
	public LoadCommand(Model model, StatusViewport statusViewport, Splasher splashViewport, AreaViewport areaViewport)
	{
		this.model = model;
		this.statusViewport = statusViewport;
		this.fileName = statusViewport.promptOpenDialog();
		this.splashViewport = splashViewport;
		this.gameCommand = new ChangeToGameViewCommand(splashViewport, statusViewport, areaViewport);
	}

	//checks if the filename is valid and calls the load command
	public void execute()
	{
		if (fileName != null)
		{
			try{
				model.load(fileName);
				splashViewport.accept(gameCommand);
			}
			catch(FileNotFoundException e)
			{
				System.out.println(e.toString());
			//	e.printStackTrace();			//for the testing path
				//hahaha I caught you
				JOptionPane.showMessageDialog(null, "File not found. Please find the right file this time.");
			} 
			//doing the catching IOException
			catch (IOException e) {
				System.out.println(e.toString());
		//		e.printStackTrace();		//for the testing path
				//hahaha I catch you too
				JOptionPane.showMessageDialog(null, "Couldn't read your file. Please check that file");
			}
		}
		if(Model.infoMode)System.out.println("[MODEL] the Load command has been called");
	}
	
	public String toString()
	{
		return this.getClass().getSimpleName();
	}

}